#include <sdk.h>
#include "formattersettings.h"
#include "astylepredefinedstyles.h"
#include <configmanager.h>

FormatterSettings::FormatterSettings() {
}

FormatterSettings::~FormatterSettings() {
}

void FormatterSettings::ApplyTo( astyle::ASFormatter& formatter ) {
  ConfigManager* cfg = Manager::Get()->GetConfigManager( _T( "astyle" ) );
  formatter.setFormattingStyle( astyle::STYLE_JAVA );
  formatter.setAttachClass( cfg->ReadBool( _T( "/attach_classes" ) ) );
  formatter.setAttachExternC( cfg->ReadBool( _T( "/attach_extern_c" ) ) );
  formatter.setAttachNamespace( cfg->ReadBool( _T( "/attach_namespaces" ) ) );
  formatter.setAttachInline( cfg->ReadBool( _T( "/attach_inlines" ) ) );
  int spaceNum = 2;
  bool value = cfg->ReadBool( _T( "/force_tabs" ) );
  if( cfg->ReadBool( _T( "/use_tabs" ) ) ) {
    formatter.setTabIndentation( spaceNum, value );
  } else {
    formatter.setSpaceIndentation( spaceNum );
  }
  int contNum = cfg->ReadInt( _T( "/continuation" ), 0 );
  if( contNum > 0 && contNum <= 4 ) {
    formatter.setContinuationIndentation( contNum );
  }
  formatter.setCaseIndent( cfg->ReadBool( _T( "/indent_case" ) ) );
  formatter.setClassIndent( true );
  formatter.setLabelIndent( true );
  formatter.setModifierIndent( true );
  formatter.setNamespaceIndent( true );
  formatter.setSwitchIndent( cfg->ReadBool( _T( "/indent_switches" ) ) );
  formatter.setPreprocBlockIndent( cfg->ReadBool( _T( "/indent_preproc_block" ) ) );
  formatter.setPreprocDefineIndent( true );
  formatter.setPreprocConditionalIndent( true );
  formatter.setIndentCol1CommentsMode( true );
  formatter.setMinConditionalIndentOption( cfg->ReadInt( _T( "/min_conditional_indent" ), 2 ) );
  formatter.setMaxInStatementIndentLength( cfg->ReadInt( _T( "/max_instatement_indent" ), 40 ) );
  formatter.setBreakClosingHeaderBracketsMode( cfg->ReadBool( _T( "/break_closing" ) ) );
  formatter.setBreakElseIfsMode( false );
  formatter.setAddBracketsMode( cfg->ReadBool( _T( "/add_brackets" ) ) );
  formatter.setAddOneLineBracketsMode( true );
  formatter.setRemoveBracketsMode( false );
  formatter.setBreakOneLineBlocksMode( false );
  formatter.setBreakOneLineHeadersMode( !cfg->ReadBool( _T( "/keep_headers" ) ) );
  formatter.setBreakOneLineStatementsMode( !cfg->ReadBool( _T( "/keep_statements" ) ) );
  formatter.setTabSpaceConversionMode( false );
  formatter.setCloseTemplatesMode( cfg->ReadBool( _T( "/close_templates" ) ) );
  formatter.setStripCommentPrefix( true );
  if( cfg->ReadBool( _T( "/break_lines" ) ) ) {
    formatter.setMaxCodeLength( wxAtoi( cfg->Read( _T( "/max_line_length" ) ) ) );
    formatter.setBreakAfterMode( cfg->ReadBool( _T( "/break_after_mode" ) ) );
  } else {
    formatter.setMaxCodeLength( INT_MAX );
  }
  formatter.setBreakBlocksMode( cfg->ReadBool( _T( "/break_blocks" ) ) );
  formatter.setBreakClosingHeaderBlocksMode( cfg->ReadBool( _T( "/break_blocks_all" ) ) );
  formatter.setOperatorPaddingMode( true );
  formatter.setParensOutsidePaddingMode( false );
  formatter.setParensInsidePaddingMode( true );
  formatter.setParensFirstPaddingMode( cfg->ReadBool( _T( "/pad_first_paren_out" ) ) );
  formatter.setParensHeaderPaddingMode( cfg->ReadBool( _T( "/pad_header" ) ) );
  formatter.setParensUnPaddingMode( true );
  formatter.setCommaPaddingMode( true );
  formatter.setDeleteEmptyLinesMode( cfg->ReadBool( _T( "/delete_empty_lines" ) ) );
  formatter.setEmptyLineFill( cfg->ReadBool( _T( "/fill_empty_lines" ) ) );
  wxString pointerAlign = cfg->Read( _T( "/pointer_align" ) );
  if( pointerAlign == _T( "Type" ) ) {
    formatter.setPointerAlignment( astyle::PTR_ALIGN_TYPE );
  } else if( pointerAlign == _T( "Middle" ) ) {
    formatter.setPointerAlignment( astyle::PTR_ALIGN_MIDDLE );
  } else if( pointerAlign == _T( "Name" ) ) {
    formatter.setPointerAlignment( astyle::PTR_ALIGN_NAME );
  } else {
    formatter.setPointerAlignment( astyle::PTR_ALIGN_NONE );
  }
  wxString referenceAlign = cfg->Read( _T( "/reference_align" ) );
  if( referenceAlign == _T( "Type" ) ) {
    formatter.setReferenceAlignment( astyle::REF_ALIGN_TYPE );
  } else if( referenceAlign == _T( "Middle" ) ) {
    formatter.setReferenceAlignment( astyle::REF_ALIGN_MIDDLE );
  } else if( referenceAlign == _T( "Name" ) ) {
    formatter.setReferenceAlignment( astyle::REF_ALIGN_NAME );
  } else {
    formatter.setReferenceAlignment( astyle::REF_ALIGN_NONE );
  }
}
